package com.xiaoxin.experience.api.ssh;

import com.xiaoxin.experience.api.ssh.request.DownloadFileRequest;
import com.xiaoxin.experience.api.ssh.request.LocalFileListRequest;
import com.xiaoxin.experience.api.ssh.request.RemoteFileListRequest;
import com.xiaoxin.experience.api.ssh.request.UploadRestartRequest;
import com.xiaoxin.experience.common.RestResponse;
import com.xiaoxin.experience.service.ssh.SSHService;
import com.xiaoxin.experience.service.ssh.model.FileInfo;
import com.xiaoxin.experience.service.ssh.model.LastQueryInfo;
import com.xiaoxin.experience.service.ssh.model.RemoteFileInfo;
import com.xiaoxin.experience.service.ssh.model.SSHSessionInfo;
import com.xiaoxin.experience.system.log.LogExecution;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xiaoxin
 */
@RestController
public class SSHController
{
    @Resource
    private SSHService sshService;

    @GetMapping("/ssh/robot/list")
    public RestResponse<List<SSHSessionInfo>> list()
    {
        return RestResponse.success(sshService.getSSHSessionInfoList());
    }

    @PostMapping("/ssh/robot/saveOrUpdate")
    public RestResponse<Boolean> saveOrUpdate(@RequestBody SSHSessionInfo sshSessionInfo)
    {
        return RestResponse.success(sshService.saveOrUpdate(sshSessionInfo));
    }

    @DeleteMapping("/ssh/robot/remove/{ip}")
    public RestResponse<Void> remove(@PathVariable String ip)
    {
        sshService.removeSession(ip);
        return RestResponse.success();
    }

    @LogExecution
    @PostMapping("/ssh/server/batchUploadRestart")
    public RestResponse<List<RestResponse<Void>>> batchUploadRestart(@RequestBody UploadRestartRequest request)
    {
        return sshService.batchUploadRestart(request.getSshSessionInfos(), request.getRemoteDirAndLocalFilePaths(), request.isRestart(), request.getFileNames());
    }

    @PostMapping("/ssh/robot/connect")
    public RestResponse<Void> connect(@RequestBody SSHSessionInfo sshSessionInfo)
    {
        return sshService.connect(sshSessionInfo);
    }

    @GetMapping("/ssh/local/file/last/query")
    public RestResponse<LastQueryInfo> lastQuery()
    {
        return RestResponse.success(sshService.getLastQueryInfo());
    }

    @PostMapping("/ssh/local/file/list")
    public RestResponse<List<FileInfo>> listLocalFile(@RequestBody LocalFileListRequest request)
    {
        return RestResponse.success(sshService.listLocalFile(request.getWorkDir(), request.getQueryMode(), request.getQuery()));
    }

    @GetMapping("/ssh/local/choose/download/path")
    public RestResponse<String> uploadLocalFile()
    {
        return sshService.getDownloadPath();
    }

    @PostMapping("/ssh/remote/file/list")
    public RestResponse<List<RemoteFileInfo>> getRemoteFileList(@RequestBody RemoteFileListRequest request)
    {
        return sshService.getRemoteFileNames(request.getSshSessionInfo(),request.getRemoteDir());
    }

    @DeleteMapping("/ssh/sftp/close/{ip}")
    public RestResponse<Void> closeSftp(@PathVariable String ip)
    {
        sshService.releaseClient(ip);
        return RestResponse.success();
    }

    @PostMapping("/ssh/sftp/download")
    public RestResponse<Void> download(@RequestBody DownloadFileRequest request)
    {
        return sshService.download(request.getSshSessionInfo(),request.getRemoteFilePath(),request.getLocalDownDir());
    }

    @PostMapping("/ssh/sftp/upload")
    public RestResponse<Void> upload(@RequestPart MultipartFile file, @RequestParam(value = "ip") String ip)
    {
        sshService.batchUploadRestart(file,ip);
        return RestResponse.success();
    }
}
